package org.openfans.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.openfans.domain.Article;
import org.openfans.domain.Facade;
import org.openfans.domain.Fans;
import org.openfans.domain.FeedItem;
import org.openfans.domain.News;
import org.openfans.domain.OtherArticle;
import org.openfans.domain.TagFeedItem;
import org.openfans.domain.TechArticle;
import org.openfans.util.ThreadFans;
import org.openfans.web.form.SaveFeedItemForm;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class SaveFeedItemFormController extends SimpleFormController {
	private Facade facade = null;

	public void setFacade(Facade facade) {
		this.facade = facade;
	}

	public SaveFeedItemFormController() {
		setCommandName("saveFeedItemForm");
		setCommandClass(SaveFeedItemForm.class);
	}

	// protected ModelAndView onSubmit(HttpServletRequest request,
	// HttpServletResponse response, Object command, BindException errors)
	// throws Exception {
	// // String id = request.getParameter("id");
	// FeedItem feedItem=(FeedItem)command;
	// String tagStr = request.getParameter("tag");
	// if (null != tagStr && !"".equals(tagStr)) {
	// tagStr = tagStr.split(" ")[0];
	// }
	// // FeedItem feedItem = facade.getFeedItem(Integer.parseInt(id));
	// Tag tag = facade.getTagByTitle(tagStr);
	// Fans fans = facade.getFans(ThreadFans.get().getFansId());
	// if (null == tag) {
	// tag = facade.addNewTagByTitle(fans, tagStr);
	// }
	// facade.addRecomArticle(fans, feedItem, tag);
	//
	// return new ModelAndView("redirect:viewFeedItem.html?id=" +
	// feedItem.getId());
	// }

	@Override
	protected ModelAndView processFormSubmission(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		// String id = request.getParameter("id");
		SaveFeedItemForm r = (SaveFeedItemForm) command;
		FeedItem feedItem = facade.getFeedItem(r.getId());
		String tagStr = r.getTagStr();
		int type = r.getType();
		Fans fans = facade.getFans(ThreadFans.get().getFansId());
		//是否已经有人保存为article了
		Article article = feedItem.getArticle();
		if (null == article) {
//			 TOTO本来应该使用factory
			if (type == 1) {
				article = new TechArticle();
			} else if (type == 2) {
				article = new News();
			} else if (type == 3) {
				article = new OtherArticle();
			}
		}

		facade.saveArticleByFeedItem(fans, feedItem, tagStr, article);
//		return new ModelAndView("redirect:viewFeedItem.html?id="
//				+ feedItem.getId());
		return null;
	}

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		ThreadFans.authorize();

		int id = Integer.parseInt(request.getParameter("id"));
		FeedItem feedItem = facade.getFeedItem(id);

		SaveFeedItemForm r = new SaveFeedItemForm();
		r.setId(id);
		r.setTitle(feedItem.getTitle());
		if (feedItem instanceof TagFeedItem) {
			r.setTagStr(((TagFeedItem) feedItem).getTag().getTitle());
		}

		return r;
	}
}
